package com.wxq.shedule.component;

import com.wxq.shedule.task.TaskExecute;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;


@Component
public class TaskManager {

    @Autowired
    private ExecutorService taskThreadPool;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    RedissonClient redisson;

    @Autowired
    TaskExecute taskExecute;

    @PostConstruct
    public void init() {
        taskThreadPool.submit(() -> {
            while (true) {
                // 获取所
                RLock lock = redisson.getLock("lock");
                System.err.println("1111");
                // 进入阻塞
                lock.lock();
                Future taskFuture = taskThreadPool.submit(taskExecute);
                taskFuture.get();
                lock.unlock();
            }
        });
    }
}
